package com.venn.service.imp;

import com.venn.controller.UserController;
import com.venn.dao.UserMapper;
import com.venn.entity.User;
import com.venn.service.UserService;
import com.venn.util.MD5Utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by venn on 2/22/17.
 */
@Service("userService")
public class UserServiceImp implements UserService{


    private static Logger logger = LogManager.getLogger(UserController.class.getName());
    /**
     * 在service定义：不需要再写setDao的方法就可以通过接口调用Dao
     */
    @Autowired
    private UserMapper userMapper;


    @Override
    public String userLogin(User user) {

        logger.error(userMapper.toString());
        // it's worked

        String id = userMapper.getUserId(user);

        return id;
    }

    @Override
    public boolean signUp(String loginName, String password) {
        password = MD5Utils.getMD5(password);
        logger.info(loginName +" sign up");
        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(password);
        userMapper.insertUser(user);

        return true;
    }

    @Override
    public List<User> queryAll() {

        List<User> users = userMapper.queryAll();


        return users;
    }

    @Override
    public void deleteUser(String id) {
        userMapper.deleteUser(id);
    }

    @Override
    public void deleteUserBatch(List<String> ids) {
        userMapper.deleteUserBatch(ids);
    }

    @Override
    public void modifyUser(User user) {
        userMapper.modifyUser(user);
    }


    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}
